//
//  SYChannel.m
//  MicroNews
//
//  Created by tongcheng on 15/10/23.
//  Copyright © 2015年 siyue. All rights reserved.
//

#import "SYChannel.h"

#define Duration 0.2
#define kChannelW 50
#define kChannelH 30
@interface SYChannel()
{
    BOOL contain;
    CGPoint startPoint;
    CGPoint originPoint;
    
}
@property (strong , nonatomic) NSMutableArray *itemArray;

/** 新闻接口的数组 */
@property (nonatomic, strong) NSArray *arrayLists;



@end
@implementation SYChannel

- (NSArray *)arrayLists
{
    if (_arrayLists == nil) {
        _arrayLists = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"WD.plist" ofType:nil]];
    }
    return _arrayLists;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.itemArray = [NSMutableArray array];
        
//        [self setupView:self.arrayLists];
//        [self setupView];
        
    }
    return self;
}


// 搭建界面，九宫格
#define kAppViewW 50
#define kAppViewH 30
#define kColCount 4
#define kStartY   10
- (void)setupView:(NSArray *)arrayLists
{

    
    // 320 - 3 * 80 = 80 / 4 = 20
    CGFloat marginX = (ScreenWidth - kColCount * kAppViewW) / (kColCount + 1);
    CGFloat marginY = 10;
    
    for (int i = 0; i < self.arrayLists.count; i++) {
        // 行
        int row = i / kColCount;
        
        // 列
        int col = i % kColCount;
        
        CGFloat x = marginX + col * (marginX + kAppViewW);
        CGFloat y = kStartY + marginY + row * (marginY + kAppViewH);
        
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, kAppViewW, kAppViewH)];
        
        
        btn.backgroundColor = SYSubjectColor;

        btn.tag = i + 1;
        btn.titleLabel.font = [UIFont systemFontOfSize:17];
        if (i == 0 || i == 1) {
            [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            btn.enabled = NO;
        }
        [btn setTitle:[NSString stringWithFormat:@"%@",arrayLists[i][@"name"]] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"quxiao"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"wode yetou BG"] forState:UIControlStateHighlighted];
        [btn setBackgroundImage:[UIImage imageNamed:@"wode yetou BG"] forState:UIControlStateSelected];
        
        
//        [btn setBackgroundImage:[UIImage imageNamed:@"Button_element"] forState:UIControlStateNormal];
//        [btn setBackgroundImage:[UIImage imageNamed:@"Button_element_Highlight"] forState:UIControlStateHighlighted];
        [btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(buttonLongPressed:)];
        [btn addGestureRecognizer:longGesture];
        [self.itemArray addObject:btn];
        
    }
    
    
    
//    NSLog(@"___%@",self.itemArray);

}

- (void)click:(UIButton *)sender
{
    NSLog(@"--><#name#>--------》%d",sender.tag);
    if (sender.tag !=1 && sender.tag != 2) {
        sender.selected = !sender.selected;
    }
    
    
   
}

- (void)setupView
{

    
    // 320 - 3 * 80 = 80 / 4 = 20
    //    int kColCount = self.GoodsClassArray.count;
    CGFloat marginX = (self.width - kColCount * kAppViewW) / (kColCount + 1);
    CGFloat marginY = 10;
    
    for (int i = 0; i < self.arrayLists.count; i++) {
        // 行
        // 0, 1, 2 => 0
        // 3, 4, 5 => 1
        int row = i / kColCount;
        // 列
        // 0, 3, 6 => 0
        // 1, 4, 7 => 1
        // 2, 5, 8 => 2
        int col = i % kColCount;
        CGFloat x = marginX + col * (marginX + kAppViewW);
        CGFloat y = kStartY + marginY + row * (marginY + kAppViewH);
        
        //         UIButton *button = [[UIButton alloc] init];
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, kAppViewW, kAppViewH)];
        [button setBackgroundImage:[UIImage imageNamed:@"Button_element"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"Button_element_Highlight"] forState:UIControlStateHighlighted];
        [button setTitle:self.arrayLists[i][@"name"] forState:UIControlStateNormal];
        [button setTitle:self.arrayLists[i][@"name"] forState:UIControlStateHighlighted];
        //all.tintColor = [UIColor redColor];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
        button.titleLabel.font = [UIFont systemFontOfSize: 13.0];
        //         button.width = 60;
        //         button.height = 30;
        //         button.x = x;
        //         button.y = y;
        button.userInteractionEnabled = YES;
        [self addSubview:button];
        
        UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(buttonLongPressed:)];
        [button addGestureRecognizer:longGesture];
        [self.itemArray addObject:button];

    }
    
    
  


}

- (void)buttonLongPressed:(UILongPressGestureRecognizer *)sender
{
    UIButton *btn = (UIButton *)sender.view;
    if (sender.state == UIGestureRecognizerStateBegan)
    {
        startPoint = [sender locationInView:sender.view];
        originPoint = btn.center;
        [UIView animateWithDuration:Duration animations:^{
            
            btn.transform = CGAffineTransformMakeScale(1.1, 1.1);
            btn.alpha = 0.7;
        }];
        
    }
    else if (sender.state == UIGestureRecognizerStateChanged)
    {
        
        CGPoint newPoint = [sender locationInView:sender.view];
        CGFloat deltaX = newPoint.x-startPoint.x;
        CGFloat deltaY = newPoint.y-startPoint.y;
        btn.center = CGPointMake(btn.center.x+deltaX,btn.center.y+deltaY);
//        NSLog(@"center = %@",NSStringFromCGPoint(btn.center));
        NSInteger index = [self indexOfPoint:btn.center withButton:btn];
        if (index<0)
        {
            contain = NO;
        }
        else
        {
            [UIView animateWithDuration:Duration animations:^{
                
                CGPoint temp = CGPointZero;
                UIButton *button = _itemArray[index];
                temp = button.center;
                button.center = originPoint;
                btn.center = temp;
                originPoint = btn.center;
                contain = YES;
//                NSLog(@"___%@",self.itemArray);
                
            }];
        }
        
        
    }
    else if (sender.state == UIGestureRecognizerStateEnded)
    {
        [UIView animateWithDuration:Duration animations:^{
            
            btn.transform = CGAffineTransformIdentity;
            btn.alpha = 1.0;
            if (!contain)
            {
                btn.center = originPoint;
            }
        }];
    }
    
   

}


- (NSInteger)indexOfPoint:(CGPoint)point withButton:(UIButton *)btn
{
    for (NSInteger i = 0;i<_itemArray.count;i++)
    {
        UIButton *button = _itemArray[i];
        if (button != btn)
        {
            if (CGRectContainsPoint(button.frame, point))
            {
                
                return i;
            }
        }
    }
    
    return -1;
}

-(void)dealloc
{
    SYNSString(@"dealloc    ");
}


@end
